package resources;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class FileReaderWriter {
	
	public FileReaderWriter(){
	}
	
	
	
	public StringBuilder ReadFile(String filename) {
		// ...checks on aFile are elided
		StringBuilder contents = new StringBuilder();
		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			BufferedReader input = new BufferedReader(new FileReader(filename));
			try {
				String line = null; // not declared within while loop
				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					contents.append(line);
					contents.append(System.getProperty("line.separator"));
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return contents;
	}

	
	public void WriteFile(String filename, String contents) {
		try {
			// use buffering
			Writer output = new BufferedWriter(new FileWriter(filename));
			// FileWriter always assumes default encoding is OK!
			output.write(contents);
			output.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
